package com.example.chat.service.impl;

import com.example.chat.constant.ErrorMessageConstant;
import com.example.chat.model.Role;
import com.example.chat.repository.RoleRepository;
import com.example.chat.service.InitService;
import com.example.chat.utils.ChatException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Date 2022/11/17 13:41
 * @Author pq
 */
@Service
public class InitServiceImpl implements InitService {

    @Resource
    private RoleRepository repository;

    public List<String> getRoleIds(){
        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("initData/role.xml");
        List<Role> roleList=(List<Role>)getXStream().fromXML(inputStream);
        return roleList.stream().map(Role::getId).collect(Collectors.toList());
    }

    @NotNull
    private XStream getXStream() {
        XStream xstream = new XStream();
        xstream.addPermission(NullPermission.NULL);
        xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
        xstream.allowTypeHierarchy(Collection.class);
        xstream.allowTypesByWildcard(new String[]{"com.example.chat.model.**"});
        return xstream;
    }
}
